package com.geeguo.ebuilder.operation.business.system.rolewebmodule.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geeguo.ebuilder.core.common.exception.ApplicationException;
import com.geeguo.ebuilder.operation.business.system.rolewebmodule.mapper.RoleWebModuleMapper;
import com.geeguo.ebuilder.operation.business.system.rolewebmodule.model.RoleWebModuleEntity;
import com.geeguo.ebuilder.operation.business.system.rolewebmodule.service.RoleWebModuleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleWebModuleServiceImpl extends ServiceImpl<RoleWebModuleMapper, RoleWebModuleEntity> implements RoleWebModuleService {

    @Autowired
    private RoleWebModuleMapper roleWebModuleMapper;

    @Override
    public List<String> listModuleIdByRole(String roleId) {
        return roleWebModuleMapper.listModuleIdByRole(roleId);
    }

    @Override
    public List<String> listModuleIdByUser(String userId) {
        return roleWebModuleMapper.listModuleIdByUser(userId);
    }

    @Override
    public boolean removeByRole(String roleId) {
        return roleWebModuleMapper.removeByRole(roleId) >= 0;
    }

    @Override
    @Transactional
    public boolean updateModule(String roleId, List<RoleWebModuleEntity> entities, List<String> deleteModuleIds) {
        boolean result = true;
        if (!CollectionUtils.isEmpty(entities)) {
            result = this.saveBatch(entities);
        }
        if (result && !CollectionUtils.isEmpty(deleteModuleIds)) {
            result = roleWebModuleMapper.removeModules(roleId, deleteModuleIds);
        }
        if (!result) {
            throw new ApplicationException("更新角色菜单失败");
        }
        return true;
    }
}